package gz.itcast.b_statement;

import gz.itcast.util.JdbcUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.Statement;

/**
 * Created 吴海杰 on 2017/2/27.
 */

public class Demo2 {
    private String url = "jdbc:mysql://localhost:3306/day0224";
    private String user = "whj";
    private String password = "whj";

    @Test
    public void testInsert() {
        Connection conn = null;
        Statement stmt = null;
        try {
/*
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(url, user, password);*/
conn=JdbcUtil.getConnection();
            stmt = conn.createStatement();
            String sql = "INSERT INTO student(name, gender) VALUES ('lisi','w');";
            int count = stmt.executeUpdate(sql);
            System.out.println("影响了" + count + "行");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

               /* if (stmt != null)
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                if (conn != null)
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }*/
               JdbcUtil.close(conn,stmt);
            }
        }




    @Test
    public void testUpdate() {
        Connection conn = null;
        Statement stmt = null;
        try {

            /*Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(url, user, password);*/
            conn= JdbcUtil.getConnection();
            stmt = conn.createStatement();
            String sql = "UPDATE student SET day0224.student.name='chenliu1'  WHERE id=2;";
            int count = stmt.executeUpdate(sql);
            System.out.println("影响了" + count + "行");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

               /* if (stmt != null)
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                if (conn != null)
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }*/
               JdbcUtil.close(conn,stmt);

        }

    }
}
